//
//  OrderCellTableViewCell.swift
//  ExpressManager
//
//  Created by sheng on 2017/11/3.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import SnapKit

protocol OrderCellDelegate: NSObjectProtocol {
    func viewOrderDetail(label: UILabel)
    
    func printOrder(label: UILabel)
    
    func signatureOrder(label: UILabel)
}

class OrderCellTableViewCell: UITableViewCell,RatingBarDelegate {
    
    weak var delegate: OrderCellDelegate?
    
    @IBOutlet weak var logoImg: UIImageView!
    
    @IBOutlet weak var priceLabel: UILabel!
    
    @IBOutlet weak var companyName: UILabel!
    
    @IBOutlet weak var companyInfo: UILabel!
    
    @IBOutlet weak var addressLabel: UILabel!
    
    @IBOutlet weak var statusLabel: UILabel!
    
    @IBOutlet weak var statusLog: UIImageView!
    
    let screenWidth = UIScreen.main.bounds.width
    
    var ratingBar : WNRatingBar?
   
  
    @IBOutlet weak var printLabel: UILabel!
    
    @IBOutlet weak var viewLabel: UILabel!
    
    @IBOutlet weak var trackLabel: UILabel!
    
    
    @IBOutlet weak var uploadLabel: UILabel!
    
    @IBOutlet weak var signatureLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setupUI()
    }
    

    func setupUI() {
        
        
        logoImg?.layer.borderColor = UIColor.gray.cgColor
        logoImg?.layer.borderWidth = 1.0
        
        logoImg?.snp.makeConstraints({ (make) in
            make.top.left.equalTo(10)
            make.width.height.equalTo(40)
        })

        statusLog?.layer.borderColor = UIColor.gray.cgColor
        statusLog?.layer.borderWidth = 1.0
        statusLog?.snp.makeConstraints({ (make) in
            make.top.equalTo(10)
            make.right.equalTo(-10)
            make.width.height.equalTo(60)
        })
        
        priceLabel?.numberOfLines = 0
        priceLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(logoImg.snp.bottom).offset(5)
            make.centerX.equalTo(logoImg.snp.centerX)
        })

        companyName?.numberOfLines = 0
        companyName?.snp.makeConstraints({ (make) in
            make.top.equalTo(10)
            make.left.equalTo(logoImg.snp.right).offset(15)
            make.right.equalTo(statusLog.snp.left).offset(-10)
        })
       
        companyInfo?.numberOfLines = 0
        companyInfo?.snp.makeConstraints({ (make) in
            make.top.equalTo(companyName.snp.bottom).offset(3)
            make.left.equalTo(companyName.snp.left)
            make.right.equalTo(companyName.snp.right)
        })
        
        addressLabel?.numberOfLines = 0
        addressLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(companyInfo.snp.bottom).offset(3)
            make.left.equalTo(companyInfo.snp.left)
            make.right.equalTo(companyInfo.snp.right)
        })
       
        statusLabel?.numberOfLines = 0
        statusLabel?.snp.makeConstraints { (make) in
            make.top.equalTo(addressLabel.snp.bottom).offset(3)
            make.left.equalTo(addressLabel.snp.left)
            make.right.equalTo(addressLabel.snp.right)
        }
        
        ratingBar = WNRatingBar()
        ratingBar?.frame = CGRect(x: 0, y: 0, width: 100, height: 12)
        ratingBar?.setSeletedState("star_big1", halfSelectedName: "star_big2", fullSelectedName: "star_big3", starSideLength: 20, delegate: self )
        ratingBar?.displayRating(8)
         self.addSubview(ratingBar!)
        
        ratingBar?.snp.makeConstraints { (make) in
            make.top.equalTo(statusLabel.snp.bottom).offset(5)
            make.left.equalTo(statusLabel.snp.left)
            make.right.equalTo(statusLabel.snp.right)
        }
        
        
        let textFont = UIFont.systemFont(ofSize: 14)
        let textMaxSize = CGSize(width: 240, height: CGFloat(MAXFLOAT))
        let viewWidth = textSize(text:"view" , font: textFont, maxSize: textMaxSize).width
        let trackWidth = textSize(text:"tracking" , font: textFont, maxSize: textMaxSize).width
        let uploadWidth = textSize(text:"upload" , font: textFont, maxSize: textMaxSize).width
        let printWidth = textSize(text:"print" , font: textFont, maxSize: textMaxSize).width
        let signatureWidth = textSize(text:"signature" , font: textFont, maxSize: textMaxSize).width
        
        
        let commdWith = (screenWidth - viewWidth - trackWidth - uploadWidth - printWidth - signatureWidth)
        
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewClick))
        viewLabel.addGestureRecognizer(tap)
        //设置能否与用户进行交互 默认是NO
        viewLabel.isUserInteractionEnabled = true
        viewLabel?.snp.makeConstraints { (make) in
            make.bottom.equalTo(-10)
            make.left.equalTo(commdWith/6)
            make.top.equalTo(statusLabel.snp.bottom).offset(28)
        }
        
        
        trackLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(-10)
            make.left.equalTo(viewLabel.snp.right).offset(commdWith/6)
            make.top.equalTo(statusLabel.snp.bottom).offset(28)
            
        }

        uploadLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(-10)
            make.left.equalTo(trackLabel.snp.right).offset(commdWith/6)
            make.top.equalTo(statusLabel.snp.bottom).offset(28)
            
        }

        let printTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(printClick))
        printLabel.addGestureRecognizer(printTap)
        //设置能否与用户进行交互 默认是NO
        printLabel.isUserInteractionEnabled = true
        printLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(-10)
            make.left.equalTo(uploadLabel.snp.right).offset(commdWith/6)
            make.top.equalTo(statusLabel.snp.bottom).offset(28)
            
        }

        let signatureTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(signatureClick))
        signatureLabel.addGestureRecognizer(signatureTap)
        //设置能否与用户进行交互 默认是NO
        signatureLabel.isUserInteractionEnabled = true
        signatureLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(-10)
            make.right.equalTo(-(commdWith/6))
            make.top.equalTo(statusLabel.snp.bottom).offset(28)
        }
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    
    func ratingChanged(_ ratingBar: WNRatingBar, newRating: Float) {
       
    }
    
    func viewClick(sender: UILabel) {
        delegate?.viewOrderDetail(label: sender)
    }
    
    func printClick(sender: UILabel) {
        delegate?.printOrder(label: sender)
    }
    
    func signatureClick(sender: UILabel) {
        delegate?.signatureOrder(label: sender)
    }
    
    func textSize(text : String , font : UIFont , maxSize : CGSize) -> CGSize{
        return text.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName : font], context: nil).size
    }
    
}
